package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketFootprintMapper;
import com.cskaoyan.market.service.MarketAdminFootprintService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MarketAdminFootprintServiceImpl implements MarketAdminFootprintService {

    @Override
    public List<MarketFootprint> list(String page, String limit, String userId, String goodsId, String sort, String order) {
            // 获取session和example
            SqlSession sqlSession = MyBatisUtil.getSession();
            MarketFootprintMapper mapper = sqlSession.getMapper(MarketFootprintMapper.class);
            MarketFootprintExample example = new MarketFootprintExample();

            MarketFootprintExample.Criteria criteria = example.or();

            if (!StringUtils.isEmpty(goodsId)) {
                criteria.andGoodsIdEqualTo(Integer.parseInt(goodsId));
            }
            if (!StringUtils.isEmpty(userId)) {
                criteria.andUserIdEqualTo(Integer.parseInt(userId));
            }
            example.setOrderByClause(sort + " " + order);

            // 设置PageHelper,并执行查询
            int pageNum = Integer.parseInt(page);
            int pageSize = Integer.parseInt(limit);
            PageHelper.startPage(pageNum, pageSize);
            List<MarketFootprint> footprints = mapper.selectByExample(example);

            // 提交并关闭sqlSession
            sqlSession.commit();
            sqlSession.close();

            return  footprints;

    }
}
